package com.java110.mall.chain.smo;


import com.java110.mall.chain.supplier.ISupplierAdapter;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.chain.ChainSupplierProductDto;
import com.java110.dto.chain.StoreOrderChainDto;
import com.java110.dto.chain.ChainSupplierDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.chain.IChainCoreInnerServiceSMO;
import com.java110.intf.chain.IChainSupplierV1InnerServiceSMO;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName ChainCoreInnerServiceSMOImpl
 * @Description 调用链核型处理类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class ChainCoreInnerServiceSMOImpl extends BaseServiceSMO implements IChainCoreInnerServiceSMO {

    public static final String DEFAULT_SUPPLIER_ADAPTER = "defaultSupplierAdapter";

    private ISupplierAdapter supplierAdapter;

    @Autowired
    private IChainSupplierV1InnerServiceSMO chainSupplierV1InnerServiceSMOImpl;


    /**
     * 供应链 下单校验单
     *
     * @param storeOrderChainDto 订单调用链数据
     * @return
     */
    @Override
    public ResultVo validateChainOrder(@RequestBody StoreOrderChainDto storeOrderChainDto) throws Exception {
        List<StoreOrderCartDto> storeOrderCartDtos = storeOrderChainDto.getStoreOrderCartDtos();
        ResultVo resultVo = new ResultVo(ResultVo.CODE_OK, "成功");
        if (storeOrderCartDtos == null || storeOrderCartDtos.size() < 1) {
            return resultVo;
        }
        ChainSupplierDto tmpChainSupplierDto = null;

        Map<String, List<StoreOrderCartDto>> maps = new HashMap<>();

        List<StoreOrderCartDto> tmpStoreOrderCartDtos = null;

        for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
            if (StringUtil.isEmpty(storeOrderCartDto.getCsId())) {
                continue;
            }
            if (maps.containsKey(storeOrderCartDto.getCsId())) {
                tmpStoreOrderCartDtos = maps.get(storeOrderCartDto.getCsId());
                tmpStoreOrderCartDtos.add(storeOrderCartDto);
                continue;
            }
            tmpStoreOrderCartDtos = new ArrayList<>();
            tmpStoreOrderCartDtos.add(storeOrderCartDto);
            maps.put(storeOrderCartDto.getCsId(), tmpStoreOrderCartDtos);
        }

        for (String csId : maps.keySet()) {

            tmpChainSupplierDto = getAdapterName(csId);
            if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
                continue;
            }
            supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
            if (supplierAdapter == null) {
                continue;
            }

            resultVo = supplierAdapter.validateChainOrder(storeOrderChainDto.getStoreOrderDto(), maps.get(csId), tmpChainSupplierDto);

            if (resultVo == null || ResultVo.CODE_OK == resultVo.getCode()) {
                continue;
            }
            return resultVo;
        }

        return resultVo;
    }

    @Override
    public ResultVo notifyChainOrder(@RequestBody StoreOrderChainDto storeOrderChainDto) throws Exception {
        List<StoreOrderCartDto> storeOrderCartDtos = storeOrderChainDto.getStoreOrderCartDtos();
        ResultVo resultVo = new ResultVo(ResultVo.CODE_OK, "成功");
        if (storeOrderCartDtos == null || storeOrderCartDtos.size() < 1) {
            return resultVo;
        }
        ChainSupplierDto tmpChainSupplierDto = null;
        Map<String, List<StoreOrderCartDto>> maps = new HashMap<>();

        List<StoreOrderCartDto> tmpStoreOrderCartDtos = null;

        for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
            if (StringUtil.isEmpty(storeOrderCartDto.getCsId())) {
                continue;
            }
            if (maps.containsKey(storeOrderCartDto.getCsId())) {
                tmpStoreOrderCartDtos = maps.get(storeOrderCartDto.getCsId());
                tmpStoreOrderCartDtos.add(storeOrderCartDto);
                continue;
            }
            tmpStoreOrderCartDtos = new ArrayList<>();
            tmpStoreOrderCartDtos.add(storeOrderCartDto);
            maps.put(storeOrderCartDto.getCsId(), tmpStoreOrderCartDtos);
        }

        for (String csId : maps.keySet()) {
            tmpChainSupplierDto = getAdapterName(csId);
            if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
                continue;
            }
            supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
            if (supplierAdapter == null) {
                continue;
            }

            supplierAdapter.notifyChainOrder(storeOrderChainDto.getStoreOrderDto(), maps.get(csId), tmpChainSupplierDto);
        }
        return resultVo;
    }

    /**
     * 查询商品
     * @param chainSupplierProductDto  订单调用链数据
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo queryProducts(@RequestBody ChainSupplierProductDto chainSupplierProductDto) throws Exception {
        ChainSupplierDto tmpChainSupplierDto = null;
        tmpChainSupplierDto = getAdapterName(chainSupplierProductDto.getCsId());
        if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
            throw new IllegalArgumentException("供应商错误");
        }
        supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
        if (supplierAdapter == null) {
            throw new IllegalArgumentException("供应商错误");
        }
        return supplierAdapter.queryProducts(chainSupplierProductDto, tmpChainSupplierDto);
    }


    /**
     * 查询商品
     * @param chainSupplierProductDto  订单调用链数据
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo querySingleProduct(@RequestBody ChainSupplierProductDto chainSupplierProductDto) throws Exception {
        ChainSupplierDto tmpChainSupplierDto = null;
        tmpChainSupplierDto = getAdapterName(chainSupplierProductDto.getCsId());
        if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
            throw new IllegalArgumentException("供应商错误");
        }
        supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
        if (supplierAdapter == null) {
            throw new IllegalArgumentException("供应商错误");
        }
        return supplierAdapter.querySingleProduct(chainSupplierProductDto, tmpChainSupplierDto);
    }


    /**
     * 查询商品评论
     * @param chainSupplierProductDto  订单调用链数据
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo queryProductComments(@RequestBody ChainSupplierProductDto chainSupplierProductDto) throws Exception {
        ChainSupplierDto tmpChainSupplierDto = null;
        tmpChainSupplierDto = getAdapterName(chainSupplierProductDto.getCsId());
        if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
            throw new IllegalArgumentException("供应商错误");
        }
        supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
        if (supplierAdapter == null) {
            throw new IllegalArgumentException("供应商错误");
        }
        return supplierAdapter.queryComments(chainSupplierProductDto, tmpChainSupplierDto);
    }
    /**
     * 查询物流信息
     * @param chainSupplierProductDto  订单调用链数据
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo queryLogisticsInfo(@RequestBody ChainSupplierProductDto chainSupplierProductDto) throws Exception {
        ChainSupplierDto tmpChainSupplierDto = null;
        tmpChainSupplierDto = getAdapterName(chainSupplierProductDto.getCsId());
        if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
            throw new IllegalArgumentException("供应商错误");
        }
        supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
        if (supplierAdapter == null) {
            throw new IllegalArgumentException("供应商错误");
        }
        return supplierAdapter.queryLogisticsInfo(chainSupplierProductDto, tmpChainSupplierDto);
    }


    /**
     * 单个商品插入
     * @param chainSupplierProductDto  订单调用链数据
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo insertSingleProduct(@RequestBody ChainSupplierProductDto chainSupplierProductDto) throws Exception {
        ChainSupplierDto tmpChainSupplierDto = null;
        tmpChainSupplierDto = getAdapterName(chainSupplierProductDto.getCsId());
        if (tmpChainSupplierDto == null || StringUtil.isEmpty(tmpChainSupplierDto.getBeanName())) {
            throw new IllegalArgumentException("供应商错误");
        }
        supplierAdapter = ApplicationContextFactory.getBean(tmpChainSupplierDto.getBeanName(), ISupplierAdapter.class);
        if (supplierAdapter == null) {
            throw new IllegalArgumentException("供应商错误");
        }
        return supplierAdapter.insertSingleProduct(chainSupplierProductDto, tmpChainSupplierDto);
    }



    /**
     * 查询 适配器名称
     *
     * @param csId
     * @return
     */
    public ChainSupplierDto getAdapterName(String csId) {

        ChainSupplierDto chainSupplierDto = new ChainSupplierDto();
        chainSupplierDto.setCsId(csId);
        List<ChainSupplierDto> chainSupplierDtos = chainSupplierV1InnerServiceSMOImpl.queryChainSuppliers(chainSupplierDto);
        if (chainSupplierDtos == null || chainSupplierDtos.size() < 1) {
            return null;
        }
        return chainSupplierDtos.get(0);
    }
}
